package com.tanhua.server.controller;

import com.tanhua.server.pojo.Answers;
import com.tanhua.server.service.TestSouleService;
import com.tanhua.server.vo.QuestionnaireListVo;
import com.tanhua.server.vo.TestResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class TestSouleController {

    @Autowired
    private TestSouleService testSouleService;

    //测灵魂-问卷列表
    @GetMapping("testSoul")
    //返回
    public ResponseEntity<List<QuestionnaireListVo>> questionnaireList() {
        try {
            List<QuestionnaireListVo> quesrtionnaireListVos = testSouleService.questionnaireList();
            return ResponseEntity.ok(quesrtionnaireListVos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //测灵魂-提交问卷
    @PostMapping("testSoul")
    public ResponseEntity<Void> submitQuestions(@RequestBody Map<String, List<Answers>> answers) {
        List<Answers> answersList = answers.get("answers");
        testSouleService.submitQuestions(answersList);
        return ResponseEntity.ok(null);
    }

    //测灵魂-查看结果
    @GetMapping(" /testSoul/report/{id}")
    public ResponseEntity<TestResultVo> testResult(@PathVariable("id") Long id){
        try {
            TestResultVo testResultVo = testSouleService.testResult(id);
            return ResponseEntity.ok(testResultVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


}
